package redisLearning;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class redisZsetDemo {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            test(jedis);
        }
    }

    private static void test(Jedis jedis) {
        jedis.flushAll();
        jedis.zadd("key", 10, "m1");
        Map<String ,Double> map = new HashMap<>();
        map.put("m2", 20.0);
        map.put("m3", 30.0);
        map.put("m4", 40.0);

        jedis.zadd("key", map);
        System.out.println(jedis.zrange("key", 0, -1));

        List<Tuple> list = jedis.zrangeWithScores("key", 0, -1);
        System.out.println(list);
        String str = list.get(0).getElement();
        double store = list.get(0).getScore();
        System.out.println(str + store);

        System.out.println(jedis.zcard("key"));

        System.out.println(jedis.zrem("key", "m1"));

        System.out.println(jedis.zrange("key", 0, -1));

        System.out.println(jedis.zscore("key", "m2"));

        System.out.println(jedis.zrank("key", "m3"));
        System.out.println(jedis.zrank("key", "m2"));
    }
}
